package com.aegean.MonitorExamSYS.center.action;

import java.lang.ref.ReferenceQueue;
import java.util.Comparator;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.aegean.MonitorExamSYS.center.business.CenterBusiness;
import com.aegean.MonitorExamSYS.center.component.ConductionRef;
import com.aegean.MonitorExamSYS.center.component.SelectConductionInfo;
import com.aegean.MonitorExamSYS.center.component.SelectExamInfo;
import com.opensymphony.xwork2.ActionSupport;

import edu.emory.mathcs.backport.java.util.Collections;

@Controller("showConductionListForCenter")
@Scope("prototype")
public class showConductionListForCenter extends ActionSupport implements
		ServletRequestAware {
	@Resource
	CenterBusiness business;
	private List<SelectConductionInfo> conductions;
	private int total;
	private int exam_Id;
	private Date startTime;
	private Date endTime;
	private int state;
	private int loadState;
	private Hashtable<Integer, ConductionRef> conductionRefs;// 用于Chche内容的存储
	private ReferenceQueue<List<SelectConductionInfo>> q;// 垃圾Reference的队列
	HttpServletRequest request;
	HttpSession session;

	private showConductionListForCenter() {
		// System.out.println("Action已经启动");
		conductionRefs = new Hashtable<Integer, ConductionRef>();
		q = new ReferenceQueue<List<SelectConductionInfo>>();
	}

	public int getLoadState() {
		return loadState;
	}

	public void setLoadState(int loadState) {
		this.loadState = loadState;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public List<SelectConductionInfo> getConductions() {
		return conductions;
	}

	public void setConductions(List<SelectConductionInfo> conductions) {
		this.conductions = conductions;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}


	public int getExam_Id() {
		return exam_Id;
	}

	public void setExam_Id(int exam_Id) {
		this.exam_Id = exam_Id;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	private void cacheEmployee(int exam_Id,
			List<SelectConductionInfo> conduction) {
		cleanCache();// 清除垃圾引用
		ConductionRef ref = new ConductionRef(exam_Id, conduction, q);
		conductionRefs.put(exam_Id, ref);
	}

	private void cleanCache() {
		// TODO Auto-generated method stub
		ConductionRef ref = null;
		while ((ref = (ConductionRef) q.poll()) != null) {
			conductionRefs.remove(ref.get_key());
		}
	}

	public void clearCache() {
		cleanCache();
		conductionRefs.clear();
		System.gc();
		System.runFinalization();
	}

	public List<SelectConductionInfo> getConduction(int ID) throws Exception {
		List<SelectConductionInfo> conduction = null;
		if (conductionRefs.containsKey(ID)) {
			//System.out.println("使用软存");
			ConductionRef ref = (ConductionRef) conductionRefs.get(ID);
			conduction = (List<SelectConductionInfo>) ref.get();
		}
		if (conduction == null) {
			//System.out.println("读取数据库");
			if (loadState == 1) {
				conductions = (List<SelectConductionInfo>) session
						.getAttribute("ConListForCenter");
			} else {
				conductions = business.showConductionList(exam_Id, state,
						startTime, endTime);
				this.cacheEmployee(exam_Id, conductions);
			}
		}
		return conductions;
	}

	class sortCollection implements Comparator {
		@Override
		public int compare(Object o1, Object o2) {
			// TODO Auto-generated method stu
			int result;
			SelectConductionInfo one = (SelectConductionInfo) o1;
			SelectConductionInfo two = (SelectConductionInfo) o2;
			result = one.getPersonCount() < two.getPersonCount() ? 1 : (one
					.getPersonCount() == two.getPersonCount() ? 0 : -1);
			if (result == 0) {
				result = one.getIncludeCount() < two.getIncludeCount() ? 1
						: (one.getIncludeCount() == two.getIncludeCount() ? 0
								: -1);
				if (result == 0) {
					result = one.getCity().compareTo(two.getCity());
					if (result == 0) {
						result = one.getRegion().compareTo(two.getRegion());
					}
				}
			}
			return result;
		}
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		session = request.getSession();
		conductions = getConduction(exam_Id);
		Collections.sort(conductions, new sortCollection());
		total = conductions.size();
		session.setAttribute("ConListForCenter", conductions);
		if (session.getAttribute("confirmPositionPersonCount") != null) {
			session.removeAttribute("confirmPositionPersonCount");//考点分配考场相应的考生数目记录session
		}
		return SUCCESS;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
	}

}
